package algorithmlearn.sort;

/**
 * 插入排序
 */
public class InsertionSort {

    public static void main(String[] args) {
        int[] array = {111,22,334,231,433,11,334,22,41,57,60};
        int[] ints = insertionSort(array);
        for (int i = 0; i < ints.length; i++) {
            System.out.print(ints[i] + "  ");
        }
    }


    public static int[] insertionSort(int[] array){
        if (array == null || array.length == 0)
            return array;
        for (int i = 1; i < array.length; i++) {
            int c = array[i];
            int preIndex = i - 1;
            while (preIndex >= 0 && c < array[preIndex]){
                array[preIndex+1] = array[preIndex];
                preIndex--;
            }
            array[preIndex+1] = c;
        }

        return array;
    }
}
